#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
from utils import log
import json
from config import etc
from controls import data_user_brief_mysql
from controls import data_card_comment_mongo
from controls import data_card_collect_mysql
from utils.session_base import ApiGuest
from utils.session_mc import BaseHandler


class GetCommentHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        #cityid = self.get_argument( 'cityid', default=99 ) #for get method test
        #cardid = self.get_argument( 'cardid', default=1 ) #for get method test
        cityid = self.ss_params['cityid']
        cardid = self.ss_params['cardid']
        log.d( 'cityid=%s, cardid=%s' % (cityid, cardid) )
        if not cityid or not cardid:
            log.w( 'parameter fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        comment = data_card_comment_mongo.get_card_comment(int(cityid), int(cardid))
        log.d ( comment )
        res = { 'idx':self.ss_idx, 'ret':0, 'msg': 'ok', 'res':comment }

        self.write( json.dumps(res) )
        self.finish()
        return

class AddCommentHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        cityid = self.ss_params['cityid']
        cardid = self.ss_params['cardid']
        userid = self.ss_params['userid']
        comment = self.ss_params['content']
        log.i( 'cityid=%s, cardid=%s, userid=%s, comment=%s' % (cityid, cardid, userid, comment) )
        if not cityid or not cardid or not comment:
            log.w( 'comment content fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        if not userid :
            userid = 0
        res = {
            'idx':self.ss_idx,
            'ret':0,
            'msg':'ok',
            'res':{}
        }
        username = ''
        if int( userid ) > 0 :
            user_brief = data_user_brief_mysql.get_user_brief_by_userid(userid)
            if user_brief:
                username = user_brief['name']
        result = data_card_comment_mongo.add_card_comment(int(cityid), int(cardid), int(userid), comment, username)
        log.d ( result )
        if not result :
            res['ret'] = 2
            res['msg'] = 'ng'
        else :
            pass
            #comment_upd_que.put({'cityid':99, 'cardid':1})
            #comment_upd_que.put({'cityid':int(cityid), 'cardid':int(cardid)})

        comment = data_card_comment_mongo.get_card_comment(int(cityid), int(cardid))
        log.d ( comment )
        res['res'] = comment

        self.write( json.dumps(res) )
        self.finish()
        return

'''
收藏或者取消收藏卡牌，需要传入userid,cityid,cardid,iscollect[0：取消收藏, 1：收藏]
'''
class CollectHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        ssid = self.ssid
        userid = self.ss_params['userid']
        cityid = self.ss_params['cityid']
        cardid = self.ss_params['cardid']
        iscollect = int ( self.ss_params['iscollect'] )
        if not ( cityid and cardid ):
            log.w( 'parameter fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        p_item = {}
        p_item['ssid'] = ssid
        p_item['userid'] = int( userid )
        p_item['cityid'] = int( cityid )
        p_item['cardid'] = int( cardid )
        # cancel collect card
        if iscollect == 0 :
            data_card_collect_mysql.delete_card_collect_item( p_item )
        # collect card
        if iscollect == 1 :
            data_card_collect_mysql.add_card_collect_item( p_item )
        
        col_count = data_card_collect_mysql.get_collect_count_by_cityid_cardid( int(cityid), int( cardid ) )

        res = { 'idx':self.ss_idx, 'ret':0, 'msg':'ok', 'res':col_count }
        self.write( json.dumps(res) )
        self.finish()
        return

'''
收藏或者取消收藏卡牌，需要传入cityid,cardid
'''
class GetCollectCountHandler( BaseHandler ) :

    @ApiGuest
    def post( self ) :
        cityid = self.ss_params['cityid']
        cardid = self.ss_params['cardid']
        if not ( cityid and cardid ):
            log.w( 'parameter fetching error' )
            res = { 'idx':self.ss_idx, 'ret':1, 'msg': etc.err_500, 'res':{} }
            self.write( json.dumps(res) )
            self.finish()
            return

        col_count = data_card_collect_mysql.get_collect_count_by_cityid_cardid( int(cityid), int( cardid ) )
        res = { 'idx':self.ss_idx, 'ret':0, 'msg':'ok', 'res':col_count }
        self.write( json.dumps(res) )
        self.finish()
        return

class ApiNotFindHandler( BaseHandler ) :
    
    def get( self ):
        raise tornado.web.HTTPError( 503 )
    @tornado.web.asynchronous
    def post( self ) :
        log.i( self )
        res = { 'idx':0, 'ret':9, 'msg':'无此功能', 'res':{} }
        self.write( json.dumps(res) )
        self.finish()

